﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using RedmineToolBar.Redmine.Downloader;
using RedmineToolBar.Utility;
namespace RedmineToolBar.Redmine.Configuration
{
    public class RedmineConfigurationBuilder : IRedmineConfiguration
    {

        private Worksheet ConfigWorksheet { get; set; }

        private RedmineConfiguration Configuration = new RedmineConfiguration();

        public RedmineConfigurationBuilder()
        {
            ConfigWorksheet = GetConfigWorksheet();
        }

        public bool ConfigurationSheetExist()
        {
            Worksheet worksheetHid = null;

            try
            {
                worksheetHid = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Configuration.REDMINE_SHEET_NAME]);

            }
            catch (Exception ex1)
            {

                Console.WriteLine(ex1.StackTrace);
            }


            if (worksheetHid == null) return false;
            else return true;
        }


        public RedmineConfiguration GetRedmineCongfiguration()
        {

            return Load();
        }

        public IRedmineConfiguration SetUser(string user)
        {
            Configuration.User = user;
            return this;
        }

        public IRedmineConfiguration SetURL(string URL)
        {
            Configuration.Url = URL;
            return this;
        }

        public IRedmineConfiguration SetPwd(string pwd)
        {
            Configuration.Pwd = pwd;
            return this;
        }

        public IRedmineConfiguration SetQuery(string id, string name, string projectID)
        {
            Configuration.QueryId = id;
            Configuration.QueryName = name;
            Configuration.QueryProjectId = projectID;

            return this;
        }


        private Worksheet GetConfigWorksheet()
        {
            Worksheet worksheetHid = null;

            try
            {
                worksheetHid = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Configuration.REDMINE_SHEET_NAME]);

            }
            catch (Exception ex1)
            {

                Console.WriteLine(ex1.StackTrace);
            }


            if (worksheetHid == null)
            {
                Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets.Add();

                worksheetHid = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
                worksheetHid.Name = Configuration.REDMINE_SHEET_NAME;
                worksheetHid.Visible = Excel.XlSheetVisibility.xlSheetVeryHidden;

            }
            return worksheetHid;
        }




        public void SaveNewConfiguration(RedmineConfiguration config)
        {
            Configuration = config;
            Save();
        }

        public void Save()
        {


            ConfigWorksheet.get_Range(Configuration.PWD_RANGE).Value2 = Configuration.Pwd;
            ConfigWorksheet.get_Range(Configuration.URL_RANGE).Value2 = Configuration.Url;
            ConfigWorksheet.get_Range(Configuration.USER_RANGE).Value2 = Configuration.User;
            ConfigWorksheet.get_Range(Configuration.QUERY_ID_RANGE).Value2 = Configuration.QueryId;
            ConfigWorksheet.get_Range(Configuration.QUERY_NAME_RANGE).Value2 = Configuration.QueryName;
            ConfigWorksheet.get_Range(Configuration.QUERY_PROJECT_ID_RANGE).Value2 = Configuration.QueryProjectId;
            ConfigWorksheet.get_Range(Configuration.EXCLUDE_DESCRIPTION_RANGE).Value2 = Configuration.ExcludeDescription;
            ConfigWorksheet.get_Range(Configuration.LAST_UPDATE_RANGE).Value2 = Configuration.LastUpdate;

        }

        private RedmineConfiguration Load()
        {
            try
            {
                Configuration.Pwd = (string)(ConfigWorksheet.get_Range(Configuration.PWD_RANGE)).Value2;
                Configuration.Url = (string)(ConfigWorksheet.get_Range(Configuration.URL_RANGE)).Value2;
                Configuration.User = (string)(ConfigWorksheet.get_Range(Configuration.USER_RANGE)).Value2;

                var value = (ConfigWorksheet.get_Range(Configuration.QUERY_ID_RANGE)).Value2;
                if (value == null) Configuration.QueryId = null;
                else Configuration.QueryId = Convert.ToString(value).Split('.')[0];

                Configuration.QueryName = (string)(ConfigWorksheet.get_Range(Configuration.QUERY_NAME_RANGE)).Value2;

                var queryProjectIdValue = ConfigWorksheet.get_Range(Configuration.QUERY_PROJECT_ID_RANGE).Value2;
                if (queryProjectIdValue == null) Configuration.QueryProjectId = null;
                else Configuration.QueryProjectId = Convert.ToString(queryProjectIdValue).Split('.')[0];

                var ExcludeDescrValue = (ConfigWorksheet.get_Range(Configuration.EXCLUDE_DESCRIPTION_RANGE)).Value2;
                if (ExcludeDescrValue == null) Configuration.ExcludeDescription = true;
                else Configuration.ExcludeDescription = Convert.ToBoolean(ExcludeDescrValue);

                var LastUpdateValue = (ConfigWorksheet.get_Range(Configuration.LAST_UPDATE_RANGE)).Value2;
                if (LastUpdateValue != null) Configuration.LastUpdate = DateTime.FromOADate(LastUpdateValue);




                return Configuration;
            }
            catch (Exception e)
            {

                throw e;
            }

        }


        public RedmineDownloader GetDownloader()
        {
            return new RedmineDownloader(Configuration);

        }


        public bool isWorkbookConnected()
        {
            Load();
            return Configuration.Url != null && Configuration.User != null && Configuration.Pwd != null;
        }


        public bool isQuerySelected()
        {
            return Configuration.QueryId != null && Configuration.QueryName != null && Configuration.QueryProjectId != null;
        }


        public IRedmineConfiguration SetExcludeDescription(bool value)
        {
            Configuration.ExcludeDescription = value;
            return this;
        }


        public bool Disconnect()
        {
            try
            {

                SaveNewConfiguration(new RedmineConfiguration()
                {
                    ExcludeDescription = false,
                    Pwd = null,
                    QueryId = null,
                    QueryName = null,
                    QueryProjectId = null,
                    Url = null,
                    User = null
                });

                RibbonUtility.SetEnable(RibbonStatus.NOT_CONFIGURED);
            }
            catch (Exception ex1)
            {

                return false;
            }
            return true;
        }


        public IRedmineConfiguration SetLastUpdate(DateTime value)
        {
            Configuration.LastUpdate = value;
            return this;
        }
    }
}
